class Backtest(object):
    def __init__(self):
        pass
    def Trade_testing(self, df, tp1, tp2, al=''):  # 回测函数，参数表说明:股票数据表,买点序列名称,卖点序列名称,返回获利序列名称
        self.Init()
        self.trade = True  # 允许交易
        myMoney = self.money2  # 总资金
        if al.strip() == '':
            na = 'property'
        else:
            na = al
        self.text = '    ----开始回测-----\n'
        i = 0
        ZB_l = []
        while i < len(df):
            close = df.close.at[i]
            if df[tp1].at[i] > 0 and self.position == False and self.trade == True:
                self.priceBuy = close
                x = int(myMoney / (self.priceBuy * (1 + self.trading_Commission)) / 100)
                self.amount = x * 100.00  # 买n手
                self.Order(df.date.at[i], '14:45:01', 1, self.code, self.amount, close)
                myMoney = myMoney - self.amount * self.priceBuy * (1.00 + self.trading_Commission)
                self.position = True
                self.priceStopLoss = self.priceBuy * (1 - self.stop_loss_range)
                self.text = self.text + '日期:' + df.date.at[i] + ' 买入:' + str(round(self.amount, 0)) + '股, 价格:' + str(
                    round(self.priceBuy, 2)) + '\n'

            if (df[tp2].at[i] > 0 and self.position == True
                    and self.trade == True):  # 卖点
                self.priceSell = close
                myMoney = myMoney + self.amount * self.priceSell * (1.00 - self.trading_Commission - self.stamp_duty)
                self.position = False
                self.Order(df['date'].at[i], '14:45:02', 2, self.code, self.amount, self.priceSell)
                self.text = self.text + '日期:' + df.date.at[i] + ' 卖出:' + str(round(self.amount, 0)) + '股, 价格:' + str(
                    round(self.priceSell, 2)) + '获利:' + str(
                    round((myMoney - self.money2) / self.money2 * 100, 2)) + '%\n'
                self.amount = 0.00

            if close <= self.priceStopLoss and self.position == True and self.trade and self.stop_loss_on:  # 止损
                self.priceSell = self.priceStopLoss - 0.01
                myMoney = myMoney + self.amount * self.priceSell * (1.00 - self.trading_Commission - self.stamp_duty)
                self.position = False
                self.stop_loss_num = self.stop_loss_num + 1
                self.Order(df['date'].at[i], '14:45:02', 2, self.code, self.amount, self.priceSell)
                self.text = self.text + '日期:' + df.date.at[i] + ' 止损:' + str(round(self.amount, 0)) + '股, 价格:' + str(
                    round(self.priceSell, 2)) + '获利:' + str(
                    round((myMoney - self.money2) / self.money2 * 100, 2)) + '%\n'
                self.amount = 0.00
                if self.stop_loss_num >= self.stop_loss_max:
                    self.trade = False

            y = (myMoney + self.amount * close - self.money2) / self.money2 * 100  # 计算总体的涨跌幅
            ZB_l.append(y)
            i = i + 1

        ZB_s = pd.Series(ZB_l)
        ZB = pd.Series(ZB_s, name=na)
        df = df.join(ZB)
        self.money = myMoney
        y = (myMoney + self.amount * close - self.money2) / self.money2 * 100
        self.text = self.text + '总投入' + str(round(self.money2, 2)) + ',最终获利幅度' + str(round(y, 0)) + '%\n'

        return df
